//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.account
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.8 帐号
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */
	public class DoAccount extends DoAPI
	{
		public function DoAccount(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_ACTIVATE:String = 'http://api.t.163.com/account/activate.json';
		public static const CMD_ACTIVATE:String = 'cmdActivate';
		
		private static const URL_UPDATE_PROFILE:String = 'http://api.t.163.com/account/update_profile.json';
		public static const CMD_UPDATE_PROFILE:String = 'cmdUpdateProfile';
		
		private static const URL_VERIFY_CREDENTIALS:String = 'http://api.t.163.com/account/verify_credentials.json';
		public static const CMD_VERIFY_CREDENTIALS:String = 'cmdVerifyCredentials';
		
		private static const URL_MESSAGE_LATEST:String = 'http://api.t.163.com/reminds/message/latest.json';
		public static const CMD_MESSAGE_LATEST:String = 'cmdMessageLatest';
		
		private static const URL_RATE_LIMIT_STATUS:String = 'http://api.t.163.com/account/rate_limit_status.json';
		public static const CMD_RATE_LIMIT_STATUS:String = 'cmdRateLimitStatus';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 网易通行证开通微博，当用户有通行证账号且未开通微博时，可以通过此API开通
		 * 
		 * @param nick_name 可选参数，昵称，如果昵称不合法，则返回400
		 * @param real_name 可选参数，真实姓名，如真实姓名不合法，则返回400
		 * @param mobile 可选参数，手机号，如手机号不合法，则返回400
		 * @param id_num 可选参数，身份证号，如身份证号不合法，则返回400
		 */		
		public function activate(nick_name:String, real_name:String, mobile:String, id_num:String):void
		{
			var paras:Array = [];
			if (nick_name != null)
				paras.push(new HttpParameter('nick_name', nick_name));
			if (real_name != null)
				paras.push(new HttpParameter('real_name', real_name));
			if (mobile != null)
				paras.push(new HttpParameter('mobile', mobile));
			if (id_num != null)
				paras.push(new HttpParameter('id_num', id_num));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_ACTIVATE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParas:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_ACTIVATE, resultHandler, 
				errorHandler, URLRequestMethod.POST,postParas);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_ACTIVATE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_ACTIVATE, params);
			}
		}
		
		/**
		 * 修改用户个人资料
		 * 
		 * @param nick_name 可选参数，昵称，如果昵称不合法，则返回400
		 * @param real_name 可选参数，真实姓名，如真实姓名不合法，则返回400
		 * @param description 可选参数，用户描述，如超过163个字符，则返回400
		 * @param province 可选参数，用户省份，如省份不合法，则返回400
		 * @param city 可选参数，用户城市，如城市不合法，则返回400
		 */		
		public function updateProfile(nick_name:String, 
									  real_name:String,
									  description:String,
									  province:String, 
									  city:String):void
		{
			var paras:Array = [];
			if (nick_name != null)
				paras.push(new HttpParameter('nick_name', nick_name));
			if (real_name != null)
				paras.push(new HttpParameter('real_name', real_name));
			if (description != null)
				paras.push(new HttpParameter('description', description));
			if (province != null)
				paras.push(new HttpParameter('province', province));
			if (city != null)
				paras.push(new HttpParameter('city', city));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_UPDATE_PROFILE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParas:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_UPDATE_PROFILE, resultHandler, errorHandler, URLRequestMethod.POST, postParas);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_UPDATE_PROFILE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_UPDATE_PROFILE, params);
			}
		}
		
		/**
		 * 判断当前用户是否验证成功并返回该用户信息
		 */		
		public function verifyCredentials():void
		{
			var paras:Array = [];
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_VERIFY_CREDENTIALS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_VERIFY_CREDENTIALS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_VERIFY_CREDENTIALS, params);
			}
		}
		
		/**
		 * 返回当前登录用户未读的新消息数量
		 */		
		public function getUnreadCount():void
		{
			var paras:Array = [];
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_MESSAGE_LATEST, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_MESSAGE_LATEST, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_MESSAGE_LATEST, params);
			}
		}
		
		/**
		 * 返回当前小时内剩余访问次数
		 */		
		public function getRateLimitStatus():void
		{
			var paras:Array = [];
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_RATE_LIMIT_STATUS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_RATE_LIMIT_STATUS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_RATE_LIMIT_STATUS, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			// decode json data result
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			// decode json data error
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
		
	}
}